The following examples show the use of the EViews programming language to perform the following tasks:
EViews 6 and later has a built-in function for calculating cumulative sums. However, you could also easily calculate such sums with a few lines of commands.
Say we have a series X for which we would like to calculate the cumulative sum. If the series does not contain NAs, you can use:
smpl @first @firstseries sum0 = xsmpl @first+1 @lastsum0 = sum0(-1) + x
If the series does contain NAs, you have two options, depending on how you would like the NAs to be treated. If you would like to continue accumulating the sum, ignoring the NA observations, you can use:
smpl @first @firstseries sum1 = @nan(x,0)smpl @first+1 @lastsum1 = sum1(-1) + @nan(x,0)smpl @all if x = nasum1 = na
If you would like to reset the sum to zero whenever an NA appears in the series you can use:
smpl @allseries sum2 = xsmpl if sum2(-1)<>na and x<>nasum2 = sum2(-1) + x
The complete program is as follows:
' three rules to compute cumulative sums with missing values 'create workfilewfcreate cumsum u 1 10 'fill dataseries xx.fill 4,2,na,4,1,3,2,na,7,5 'for series without NAssmpl @first @firstseries sum0 = xsmpl @first+1 @lastsum0 = sum0(-1) + x 'ignore NAssmpl @first @firstseries sum1 = @nan(x,0)smpl @first+1 @lastsum1 = sum1(-1) + @nan(x,0)smpl @all if x = nasum1 = na 'reset to zero at NAssmpl @allseries sum2 = xsmpl if sum2(-1)<>na and x<>nasum2 = sum2(-1) + x smpl @allshow x sum0 sum1 sum2
The following sample output shows the differences between the three methods:

Many operations in EViews can be performed on a subset of the workfile observations using the smpl command. To estimate an equation using only observations which have a positive value of X, you could use:
smpl if x>0eq1.ls y c x
Time series operations often rely on observations being adjacent, so that this method may not work as expected. In this example we demonstrate a general technique for working with a subset of observations from the workfile as though they were adjacent. There are three steps to using the method:
Consider the simple case of creating a series which contains the differences between consecutive positive values of the series X, ignoring any intervening negative values.
The commands:
smpl if x>=0series dxp = d(x)
will not compute the desired values, since the d function computes differences between adjacent values, not between successive values in the sample.
The SUBSET.PRG program solves this problem using the general technique outlined above. The solution is stored in the series DX. The series X_S and DX_S are left in the workfile so you can follow the intermediate calculations.
' time series operation on subsamples ' create workfilewfcreate subset u 1 10 ' fill dataseries xx.fill 4,2,na,4,1,3,2,na,7,5 ' create samplesample ss if x<>NA and x>=0 ' create short x seriessmpl @allvector temp stomna(x, temp, ss)series x_smtos(temp, x_s)!rows = @rows(temp) 'save number of elementsdelete temp ' difference short x seriesseries dx_s = d(x_s) ' map back into long series dxvector tempstomna(dx_s, temp)vector(!rows) temp 'trim to number of elementsseries dxmtos(temp, dx, ss)delete temp ' display resultsshow x dx x_s dx_s
It is sometimes useful to create a set of dummy variables for a range of observations in a workfile. You can then include one or more of these dummies in an estimation simply by adding their names to the list of exogenous variables. It is easy to create these series by hand, though somewhat tedious.
The program MAKE_DUM.PRG automates this task. It should be run from the command line with arguments for the first and the last observation for which to create dummies. For example, to create dummies in a quarterly workfile from the first quarter of 1982 to the last quarter of 1983, the command would be:
run make_dum 1982:1 1983:4
As is, the program should work for annual, semiannual, quarterly, monthly and undated workfiles. It should be modified for weekly and daily workfiles.
Note that from version 6 of EViews onwards, you could create such dummy variables more easily using the @expand command.
' create dummy variables for every observation in sample 'create workfilewfcreate dumtest q 1970 1990 'set up start and end dates%start = "1972:1" 'first observation to dummy%end = "1979:4" 'last observation to dummy 'generate dummy variables from 'start' to 'end'for !i = @dtoo(%start) to @dtoo(%end) 'string containing observation offset %obsstr = @otod(!i) 'name of dummy if (@mid(%obsstr, 5, 1) = ":") then %name = "d_" + @left(%obsstr, 4) + "_" + @mid(%obsstr, 6) else %name = "d_" + %obsstr endif 'generate dummy series smpl @all series {%name} = 0 smpl {%obsstr} {%obsstr} series {%name} = 1next smpl @allshow d_*